<template>
  <div class="container">
    <van-datetime-picker
      v-model="localDate"
      type="date"
      title="选择出生日期"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="upload"
    />
  </div>
</template>

<script>
import { setUserProfile } from '@/api/user'
import dayjs from 'dayjs'
export default {
  methods: {
    async  upload () {
      this.$toast.loading({
        message: '修改中...',
        forbidClick: true,
        duration: 0
      })
      const date = dayjs(this.localDate).format('YYYY-MM-DD')
      try {
        await setUserProfile({ birthday: date })
        this.$emit('input', date)
        this.$toast.success('修改成功')
      } catch (err) {
        this.$toast.fail('修改失败')
      }
      this.$emit('close')
    }
  },
  data () {
    return {
      localDate: new Date(this.value),
      minDate: new Date('1970-1-1'),
      maxDate: new Date()
    }
  },
  props: {
    value: {
      type: String,
      required: true
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background: #ffffff !important;
}
.container {
  background: rgb(250, 250, 250);
  height: 100%;
}
</style>
